# -*- coding: utf-8 -*-
# 
# $Id$
# 

import socket
import sys
import array    # C interface
import time
import struct

sys.path.append('..')

def print_array(arr, length=16):
    for i in range(length):
        print('%02X'%(arr[i]), end='')
        if (i%16)==7:
            print('-',)
        if (i%16)==15:
            print()
    print()

if __name__ == '__main__':
    port = 8002
    if (len(sys.argv) > 1):
        port = int(sys.argv[1])

    #UDP
    s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)  

    s.settimeout(5) #
    s.bind(('', port))  
    print("Waiting... @", port)
    old_a = 1
    while 1:  
        try:  
            s.settimeout(5)  
            d,a=s.recvfrom(8192)  
            if (old_a != a):
                print('from', a)
                old_a = a

            arr = array.array('B', d)
            (data, length) = arr.buffer_info()

            print_array(arr, length)
            
        except socket.timeout:  
            print(".", end='')
